﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using YEngineLib;
using YEngineLib.Graphics;
using YEngineLib.Input;
using YEngineLib.State;
using YEngineLib.Util;

using Spyfall.Messages;
using Spyfall.Objects;

namespace Spyfall.Managers
{
    class LevelManager
    {
        float[] grid = { 60, 200, 360, 520, 680, 840, 1000, 1160 };

        Random rand = new Random();

        private Texture2D background;

        private float timerBox = 0.0f;
        private float timerWing = 0.0f;
		private float timerBird = 0.0f;
        private float timerBaloon = 0.0f;
        private float timerProp = 0.0f;
        private float timerGoldBalloon = 0.0f;
        private float timerCoin = 0.0f;

        float[] slots;

        public Texture2D Background
        {
            get { return background; }
            set { background = value; }
        }
        private float length;

        public float Length
        {
            get { return length; }
            set { length = value; }
        }
        private float spawnRate;

        public float SpawnRate
        {
            get { return spawnRate; }
            set { spawnRate = value; }
        }
        private float powerUpChance;

        public float PowerUpChance
        {
            get { return powerUpChance; }
            set { powerUpChance = value; }
        }
        private float speedUpSpawn;

        public float SpeedUpSpawn
        {
            get { return speedUpSpawn; }
            set { speedUpSpawn = value; }
        }
        private int speedUpNum;

        public int SpeedUpNum
        {
            get { return speedUpNum; }
            set { speedUpNum = value; }
        }

        private static LevelManager s_Instance;

        public static LevelManager GetInstance
        {
            get
            {
                if (s_Instance == null)
                    s_Instance = new LevelManager();
                return s_Instance;
            }
        }
        
        public void Update(float deltaTime)
        {
            timerBox += deltaTime;
            timerWing += deltaTime;
            timerBaloon += deltaTime;
            timerBird += deltaTime;
            timerProp += deltaTime;
            timerGoldBalloon += deltaTime;
            timerCoin += deltaTime;

            for (int i = 0; i < 8; i++)
            {
                slots[i] += deltaTime;
            }

            if (timerBox >= 2.0f)
            {
                int r = rand.Next(5);
                if (r == 0 || r == 3)
                    Spawn(0);
                else if (r == 1)
                    Spawn(6);
                else if (r == 2)
                    Spawn(7);
                else
                    Spawn(8);

                timerBox = 0.0f;
            }

            if (timerWing >= 4.0f)
            {
                Spawn(1);
                timerWing = 0.0f;
            }

            if (timerBaloon >= SpeedUpSpawn)
            {
                Spawn(2);
                timerBaloon = 0.0f;
            }

            if (timerProp >= 2.5f)
            {
                Spawn(3);
                timerProp = 0.0f;
            }

            if (timerBird >= 10.0f)
            {
                Spawn(4);
                timerBird = 0.0f;
            }

            if (timerGoldBalloon >= 60.0f)      //10.0f to test
            {
                Spawn(5);
                timerGoldBalloon = 0.0f;
            }
            if (timerCoin >= 30.0f)
            {
                Spawn(9);
                timerCoin = 0.0f;
            }
        }

        public void Initialize()
        {   
            slots = new float[8];
            for (int i = 0; i < slots.Length; i++)
            {
                slots[i] = SpawnRate;
            }

            timerBox = 0.0f;
            timerWing = 0.0f;
		    timerBird = 0.0f;
            timerBaloon = 0.0f;
            timerProp = 0.0f;
            timerGoldBalloon = 0.0f;
            timerCoin = 0.0f;
        }

        public void Spawn(int item)
        {
            if (item == 0)  //0 = Box
            {
                int randomSpawnBox = rand.Next(8);
                if (slots[randomSpawnBox] >= spawnRate)
                {
                    ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateBox, grid[randomSpawnBox], -150);
                    MessageManager.GetInstance.SendMessage(obstical);
                    slots[randomSpawnBox] = 0.0f;
                }
            }
            else if (item == 1)  //1 = Wing
            {
                int randomSpawnWing = rand.Next(7);
                if (slots[randomSpawnWing] >= spawnRate)
                {
                    ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateWing, grid[randomSpawnWing], -150);
                    MessageManager.GetInstance.SendMessage(obstical);
                    slots[randomSpawnWing] = 0.0f;
                    slots[randomSpawnWing + 1] = 0.0f;
                }
            }
            else if (item == 2)     //2 Balloon
            {
                int randspawn = rand.Next(8);
                ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateBalloon, grid[randspawn], YEngine.Graphics.ScreenViewport.Height + 10);
                MessageManager.GetInstance.SendMessage(obstical);
                slots[randspawn] = 0.0f;
            }
            else if (item == 3)     //3 = Propeller
            {
                int randSpawnPropeller = rand.Next(8);
                if (slots[randSpawnPropeller] >= spawnRate)
                {
                    ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreatePropeller, grid[randSpawnPropeller], -150);
                    MessageManager.GetInstance.SendMessage(obstical);
                    slots[randSpawnPropeller] = 0.0f;
                }

            }
            else if (item == 4)     //4 = Birds
            {
                int r = rand.Next(2);
                if( r == 0 )
                {
                    ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateBirds, -100, 260);
                    MessageManager.GetInstance.SendMessage(obstical);
                }
                else
                {
                    ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateBirds, -100, 470);
                    MessageManager.GetInstance.SendMessage(obstical);
                }
            }
            else if (item == 5)     //5 = Gold Balloon
            {
                int randspawn = rand.Next(8);
                ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateGoldBalloon, grid[randspawn], YEngine.Graphics.ScreenViewport.Height + 10);
                MessageManager.GetInstance.SendMessage(obstical);
                slots[randspawn] = 0.0f;
            }
            else if (item == 6)
            {
                int r = rand.Next(8);
                if (slots[r] >= spawnRate)
                {
                    ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateSeat, grid[r], -150);
                    MessageManager.GetInstance.SendMessage(obstical);
                    slots[r] = 0.0f;
                }
            }
            else if (item == 7)
            {
                int r = rand.Next(8);
                if (slots[r] >= spawnRate)
                {
                    ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateExit, grid[r], -150);
                    MessageManager.GetInstance.SendMessage(obstical);
                    slots[r] = 0.0f;
                }
            }
            else if (item == 8)
            {
                int r = rand.Next(8);
                if (slots[r] >= spawnRate)
                {
                    ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateEngine, grid[r], -150);
                    MessageManager.GetInstance.SendMessage(obstical);
                    slots[r] = 0.0f;
                }
            }
            else if (item == 9)
            {
                int r = rand.Next(2);
                if (r == 0)
                {
                    ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateCoin, 0, 0);
                    MessageManager.GetInstance.SendMessage(obstical);
                }
                else
                {
                    ObsticalSpawnMessage obstical = new ObsticalSpawnMessage(MSG_TYPE.Msg_CreateCoin, 1280, 0);
                    MessageManager.GetInstance.SendMessage(obstical);
                }
            }
        }
    }
}
